/* Copyright 2008 Valentin Jacquemin

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;
using Alexandrie.Model;
using System.Drawing.Imaging;
using System.IO;

namespace Alexandrie
{
    [WriterPlugin("6B9B2BAE-7E3C-4f46-AFFB-68AFD250D0B8", "Alexandria", Description = "Allow to query Amazon store to retrieve books information")]
    [InsertableContentSource("Alexandria", SidebarText = "a book definition from Amazon store")]
    public class Plugin : WindowsLive.Writer.Api.ContentSource
    {

        public static Plugin plugin;

        public enum Store
        {
            com = 0, uk, fr, ca, de, jp
        }

        public static String startStore = "Amazon.";

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            Options.SetString("awsKey", "0FDD08NC1PZWK5ANBN82");
            Options.SetString("store", Options.GetString("store", Plugin.Store.com.ToString()));
            plugin = this;
        }

        override public DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            AlexandrieInsertForm lForm = new AlexandrieInsertForm();
            DialogResult lResult = lForm.ShowDialog(dialogOwner);

            if (lResult.Equals(DialogResult.OK))
            {
                Book lSelectedBook = lForm.selectedBook;
                String lTempFileName = System.IO.Path.GetTempFileName() + ".jpg";
                lSelectedBook.image.Save(lTempFileName, ImageFormat.Jpeg);

                content = "<img src='" + lTempFileName + "' style='float: left;'/><span><b>" + lSelectedBook.titleBook + " - <i>" + lSelectedBook.author + "</i></b></span>";
                return DialogResult.OK;
            }
            else if (lResult.Equals(DialogResult.Cancel))
            {
                return DialogResult.Cancel;
            }

            PluginDiagnostics.DisplayError("Error", "Form return unexpected (neither ok nor cancel");
            return DialogResult.Abort;
        }


        public IProperties getOptions()
        {
            return this.Options;
        }
    }
}
